package com.loveshare.imgshow.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.loveshare.imgshow.dao.ImgShowDao;
import com.loveshare.imgshow.entity.Doubanmz;
import com.loveshare.imgshow.entity.Music;
import com.loveshare.imgshow.entity.VideoListInfo;
import com.loveshare.imgshow.util.DownloadFileUtil4File;
import com.loveshare.imgshow.util.HttpRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * ClassName: ImgShowService 
 * @Description: 图片展示业务类接口实现类
 * @author 爱分享
 * @date 2017年12月20日
 */
@Service
public class ImgShowServiceImpl implements ImgShowService{
	
	@Autowired
	private ImgShowDao imgShowDao;

	public List<Doubanmz> getDoubanmzs(String tableName ,int pictureType, int page, int pageSize) {
		return imgShowDao.getDoubanmzs(tableName,pictureType, page, pageSize);
	}

	public int addDoubanmz(Doubanmz doubanmz) {
		return imgShowDao.addDoubanmz(doubanmz);
	}

	public List<VideoListInfo> getVideoListInfo(int page, int pageSize) {
		return imgShowDao.getVideoListInfo(page, pageSize);
	}

	public List<Music> getMusics(String keyword, int page, int pageSize) {
		List<Music> musicList = imgShowDao.getMusics(keyword, page, pageSize);
		int searchLogCount = imgShowDao.selectMusicSearchLogCount(keyword);
		if(searchLogCount > 0){
			imgShowDao.updateMusicSearchLog(keyword);// 根据搜索日志次数
		}else{
			imgShowDao.addMusicSearchLog(keyword);// 添加搜索日志
		}
		if (musicList == null || musicList.size() == 0){
			musicList = this.getUrlMusics(keyword);
		}
		return musicList;
	}
	
	/**
	 * 从url里面拿音乐数据
	 * @return
	 */
	private List<Music> getUrlMusics(String keyword){
		List<Music> musicList = new ArrayList<Music>();
		Music musicInfo = null;
		List<Music> music4AddInfos = new ArrayList<Music>();
		try {
			//获取搜索歌曲内容
			String htmlWenZiStr = HttpRequest.sendGet("https://api.apiopen.top/searchMusic", "name="+java.net.URLEncoder.encode(keyword, "UTF-8"));
			JSONObject json = JSONObject.fromObject(htmlWenZiStr);
			JSONArray musics = JSONArray.fromObject(json.getString("result"));
			for (int i = 0; i < musics.size(); i++) {
				JSONObject music = musics.getJSONObject(i); // 遍历 jsonarray
				InputStream inputStream = DownloadFileUtil4File.getFileInputStream(music.getString("url"));
				if (inputStream == null) continue;
				musicInfo = new Music();
				musicInfo.setAuthor(music.getString("author"));
				musicInfo.setLink(music.getString("link"));
				musicInfo.setLrc(music.getString("lrc"));
				musicInfo.setPic(music.getString("pic"));
				musicInfo.setType(music.getString("type"));
				musicInfo.setTitle(music.getString("title"));
				musicInfo.setLrc(music.getString("lrc"));
				musicInfo.setSongid(music.getString("songid"));
				musicInfo.setUrl(music.getString("url"));
				musicInfo.setQnUrl(music.getString("url"));
				music4AddInfos.add(musicInfo);
				musicList.add(musicInfo);
			}
		} catch (Exception e) {
		}	
		
		return musicList;
	}
	
}
